﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class ApplyForforumAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string ForumId = null;
            if (Request.QueryString["ForumId"] != null)
                ForumId = Request.QueryString["ForumId"].ToString();
            else
                Response.Redirect("~/ForumHomePage.aspx");
            ForumIdHiddenField.Value = ForumId;
            if (GND.ReadNivdalBoolData("@ForumId", "Nivdal_ForumIsForumPublic", ForumId))
                Response.Redirect("~/ForumHomePage.aspx");
            if (!GND.ReadNivdalBoolData("@UserId", "@ForumId", "Nivdal_ForumIsUserForumMember", ou.UserId, ForumId))
                Response.Redirect("~/ForumHomePage.aspx");
            if (GND.ReadNivdalIntData("@UserId", "@ForumId", "Nivdal_ForumGetMembershipType", ou.UserId, ForumId).ToString() != "1")
                Response.Redirect("~/ForumHomePage.aspx");
            string ForumName = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumName", ForumId);
            this.ForumLinkButton.Text = ForumName;
            this.PageNameLabel2.Text = this.ForumLinkButton.Text;
            this.ForumLinkButton.PostBackUrl = "ForumDetails.aspx?Id=" + ForumId;
            //Response.Write(GND.ReadNivdalIntData("@UserId", "@ForumId", "Nivdal_ForumGetMembershipType", UsrId, ForumId).ToString());
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ApplyForforumAdmin_PageTitle + ": " + ForumName;
        }

        protected void SubmitApplicationButton_Click(object sender, EventArgs e)
        {
            string ForumId = ForumIdHiddenField.Value;

            OnlineUser ou = (OnlineUser)Session["User"];
            String ApplicationNote = ApplyForforumAdminTextArea.Text.Trim();


            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumApplyForForumAdminPosition";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_UserId = new SqlParameter();
            dbParam_UserId.ParameterName = "@UserId";
            dbParam_UserId.Value = ou.UserId;
            dbParam_UserId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UserId);

            SqlParameter dbParam_ForumId = new SqlParameter();
            dbParam_ForumId.ParameterName = "@ForumId";
            dbParam_ForumId.Value = ForumId;
            dbParam_ForumId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ForumId);

            SqlParameter dbParam_ApplicationNote = new SqlParameter();
            dbParam_ApplicationNote.ParameterName = "@ApplicationNote";
            dbParam_ApplicationNote.Value = ApplicationNote;
            dbParam_ApplicationNote.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ApplicationNote);


            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                Response.Redirect("ForumDetails.aspx?Id=" + ForumId + "&Answer=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}